/* 
 *  Syntelos XB
 *  Copyright (C) 2006 John Pritchard.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307 USA
 */

package syntelos.xb.at;

/** 
 *  <p> Child of {@link syntelos.xb.Method} {@link
 *  syntelos.xb.Attributes} uses {@link
 *  syntelos.xb.op.Code}. </p>
 * 
 * @see syntelos.xb.Method
 * @see syntelos.xb.op.Code
 * @see syntelos.xb.op.Exceptions
 * 
 * @author jdp
 */
public final class Code 
    extends syntelos.xb.Attribute
{
    public final static java.lang.String ATNAME = "Code";
    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/at","xat:code");

    /**
     * <p>  </p>
     */
    public final static class MaxStack
        extends syntelos.xb.op.Operand
    {
        public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/at","xat:code.max-stack");

        public MaxStack(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
            super(doc,name);
        }
        public MaxStack(org.w3c.dom.Document doc){
            this(doc,NAME);
        }
        public MaxStack(){
            this(DOC_NIL);
        }

        public syntelos.iou.dom.Node compile(){

            if (null == this.getChildText())
                this.setChildText("9");

            return super.compile();
        }
        public Code getAttributeCode(){
            return (Code)this.getParentNode();
        }
        public LocalVariableTable getAttributeLocalVariableTable(){
            return this.getAttributeCode().getAttributeLocalVariableTable();
        }
        public syntelos.xb.op.Code getOpCode(){
            return this.getAttributeCode().getOpCode();
        }
        public syntelos.xb.Method getMethod(){
            return this.getAttributeCode().getMethod();
        }
    }

    /**
     * <p>  </p>
     */
    public final static class MaxLocals
        extends syntelos.xb.op.Operand
    {
        public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/at","xat:code.max-locals");

        public MaxLocals(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
            super(doc,name);
        }
        public MaxLocals(org.w3c.dom.Document doc){
            this(doc,NAME);
        }
        public MaxLocals(){
            this(DOC_NIL);
        }

        public syntelos.iou.dom.Node compile(){
            int count = this.getAttributeLocalVariableTable().countChildNodes();
            this.setOperand(count);
            //
            return super.compile();
        }
        public Code getAttributeCode(){
            return (Code)this.getParentNode();
        }
        public LocalVariableTable getAttributeLocalVariableTable(){
            return this.getAttributeCode().getAttributeLocalVariableTable();
        }
        public syntelos.xb.op.Code getOpCode(){
            return this.getAttributeCode().getOpCode();
        }
        public syntelos.xb.Method getMethod(){
            return this.getAttributeCode().getMethod();
        }
    }

    public Code(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
        super(doc,name);
    }
    public Code(org.w3c.dom.Document doc){
        this(doc,NAME);
    }
    public Code(){
        this(DOC_NIL);
    }

    public syntelos.iou.dom.Node compile(){

        this.getAttributeName().setNodeValue("utf8:Code");

        this.getMaxStackOperand();

        this.getMaxLocalsOperand();

        return super.compile();
    }
    public syntelos.xb.Attributes getAttributesChild(){
        syntelos.xb.Attributes attributes = (syntelos.xb.Attributes)this.getChildByName("attributes");
        if (null == attributes){
            attributes = new syntelos.xb.Attributes();
            this.appendChild(attributes);
        }
        return attributes;
    }
    /** @see syntelos.xb.Base
     */
    public syntelos.xb.Method getMethod(){
        return (syntelos.xb.Method)(this.getParentNode().getParentNode());
    }
    /** @see syntelos.xb.Base
     */
    public syntelos.xb.at.Code getAttributeCode(){
        return this;
    }
    /** @see syntelos.xb.Base
     */
    public syntelos.xb.at.LocalVariableTable getAttributeLocalVariableTable(){
        return this.getAttributesChild().getLocalVariableTable();
    }
    public syntelos.xb.at.LineNumberTable getAttributeLineNumberTable(){
        return this.getAttributesChild().getLineNumberTable();
    }
    /** @see syntelos.xb.Base
     */
    public syntelos.xb.op.Code getOpCode(){
        syntelos.xb.op.Code code = (syntelos.xb.op.Code)this.getChildByName("code");
        if (null == code){
            code = new syntelos.xb.op.Code();
            this.appendChild(code);
        }
        return code;
    }
    public syntelos.xb.op.Exceptions getOpExceptions(){
        syntelos.xb.op.Exceptions code = (syntelos.xb.op.Exceptions)this.getChildByName("exceptions");
        if (null == code){
            code = new syntelos.xb.op.Exceptions();
            this.appendChild(code);
        }
        return code;
    }
    public MaxStack getMaxStackOperand(){
        MaxStack number = (MaxStack)this.getChildByName("code.max-stack");
        if (null == number){
            number = new MaxStack();
            this.appendChild(number);
        }
        return number;
    }
    public int getMaxStack(){
        return this.getMaxStackOperand().getOperand();
    }
    public void setMaxStack(int value){
        this.getMaxStackOperand().setOperand(value);
    }
    public MaxLocals getMaxLocalsOperand(){
        MaxLocals number = (MaxLocals)this.getChildByName("code.max-locals");
        if (null == number){
            number = new MaxLocals();
            this.appendChild(number);
        }
        return number;
    }
    public int getMaxLocals(){
    	return this.getMaxLocalsOperand().getOperand();
    }
    public void setMaxLocals(int value){
        this.getMaxLocalsOperand().setOperand(value);
    }
    public syntelos.xb.Op getOp(syntelos.xb.IO.Uri uri){
        return this.getOpCode().getOp(uri);
    }
    public syntelos.xb.Op getOpByPC(int pc){
        return this.getOpCode().getOpByPC(pc);
    }
    public syntelos.xb.Op getOpByIndex(int idx){
        return this.getOpCode().getOpByIndex(idx);
    }
    public int indexOp(syntelos.xb.Op instr){
        return this.getOpCode().indexOp(instr);
    }
    public int countOp(){
        return this.getOpCode().countOp();
    }
    public int atlen(){
        return (4 + this.getOpCode().atlen() + this.getOpExceptions().atlen() + this.getAttributesChild().atlen());
    }
    public void readCF(syntelos.xb.IO.Uri src, syntelos.xb.CF.Reader reader)
        throws java.io.IOException
    {
        int at_len = reader.readInt();
        int max_stack = reader.readUnsignedShort();
        this.setMaxStack(max_stack);
        int max_locals = reader.readUnsignedShort();
        this.setMaxLocals(max_locals);
        this.getOpCode().readCF(src,reader);
        this.getOpExceptions().readCF(src,reader);
        this.getAttributesChild().readCF(src,reader);
    }
    public void writeCF(syntelos.xb.IO.Uri dst, syntelos.xb.CF.Writer writer)
        throws java.io.IOException
    {
        this.getAttributeName().writeCF(dst,writer);
        int at_len =  this.atlen();
        writer.writeInt(at_len);
        int max_stack = this.getMaxStack();
        writer.writeShort(max_stack);
        int max_locals = this.getMaxLocals();
        writer.writeShort(max_locals);
        this.getOpCode().writeCF(dst,writer);
        this.getOpExceptions().writeCF(dst,writer);
        this.getAttributesChild().writeCF(dst,writer);
    }
}
